<?php
require_once ('./Model/Inscrits.php');
include_once('./MyPDO.php');
include_once('./Controller/Template.php');

class Controller_Connection extends Controller_Template{
	
    	protected function __construct(){
		parent::__construct();
		$this->selfModel = new Model_Inscrit(parent::$db);
		
	}
        
        public function deconnection(){
		session_unset ();
		session_destroy ();
                header('Location: index.php');
        }
           
    
    
	public function connection(){
	
		$title = utf8_decode("Connexion");
		// $title pourra être utilisé dans les scripts et les templates incorporé dans ce script PHP par les fonctions "include" ou "require"
		header('Content-Type: text/html; charset=utf-8');
		
		require './View/header.tpl';
		require './View/Index/connexion.tpl';
		       
		
		if(!empty($_POST)) {
			extract($_POST);
                        $pseudo = $_POST['Identifiant'];
                        $mdp = hash("sha512",($_POST['Mot_de_passe']));
			if((!empty($pseudo)) && (!empty($mdp))){
				$sqlReturn = $this->selfModel->connexion($pseudo, $mdp);
				if ($sqlReturn == 0){
					echo "Erreur durant l'authentification !";
				} else {
					$_SESSION['login'] = $sqlReturn[0]['login'];
					$_SESSION['prenom'] = $sqlReturn[0]['prenom'];
					$_SESSION['nom'] = $sqlReturn[0]['nom'];
					$_SESSION['avatar'] = $sqlReturn[0]['avatar'];
					$_SESSION['dateNaissance'] = $sqlReturn[0]['dateNaissance'];
					header('Refresh: 3; URL= index.php');  
				}    
			}
			else{
				echo "Il reste au moins un champ non rempli ! ";
			}        
		}       
		require './View/footer.tpl';
	}
	    
	
}

